import os
import re

# kui-novel-arrange
# 奎歪歪的小说整理 脚本
# 段落整理（旧版）

def format_text_files(directory):
    file_count = 0
    formatted_count = 0

    # 统计目录下的txt文件数量
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            file_count += 1

    # 遍历目录下的txt文件
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            filepath = os.path.join(directory, filename)
            with open(filepath, "r", encoding="utf-8") as file:
                content = file.read()

            # 格式整理
            formatted_content = ""
            paragraphs = re.split("\n\n+", content)
            for paragraph in paragraphs:
                indented_paragraph = "  " + paragraph.strip()
                formatted_content += indented_paragraph + "\n\n"

            # 将整理后的内容写回文件
            with open(filepath, "w", encoding="utf-8") as file:
                file.write(formatted_content)

            formatted_count += 1

            # 显示整理进度
            print(f"正在整理文件: {filename}")
            print(f"已整理完的文件数量: {formatted_count}/{file_count}")

    print("整理完成！")

# 指定目录路径
directory_path = "txt"

format_text_files(directory_path)